package net.huosen.blog.action;


import javax.annotation.Resource;

import net.huosen.blog.model.User;
import net.huosen.blog.service.IUserService;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
/*
 * 处理用户操作的Action类
 */
@Scope("prototype")
@Component("userAction")
public class UserAction extends ActionSupport{
	
	private final static Logger log = LogManager.getLogger(UserAction.class.getSimpleName());
	private static final long serialVersionUID = -7574227471454774126L;
	private User user = new User();
	
	@Resource(name="userService")
	public IUserService service;
	
	public String login() {
		User newUser= null;
		try{
			newUser = service.login(user);
		}catch(Exception e){
			log.error("登录出错：", e);
		}
		
		if(newUser != null){
			return SUCCESS;
		}else{
			this.addFieldError("name", "密码不正确");
			return INPUT;
		}
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public void validateLogin(){
		
		if(null == user.getName()||"".equals(user.getName().trim())){
			this.addFieldError("name", "用户名不能为空！");
		}
	}
}